<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png">
      </div>
      <!--登录表单区域-->
      <el-form ref="loginFormRef" class="login_form" :model="loginForm" :rules="loginFormRules" label-width="0px">
        <!--用户名-->
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            prefix-icon="iconfont icon-user"
            v-model="loginForm.username"
          >
          </el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="iconfont icon-3702mima"
            v-model="loginForm.password"
            type="password"
          >
          </el-input>
        </el-form-item>
        <!--按钮区域-->
        <el-form-item class="login_btn">
          <el-button type="primary" @click="onLogin">登录</el-button>
          <el-button @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin', // 用户名
        password: '123456' // 密码
      },
      // 登录表单校验
      loginFormRules: {
        username: [
          {
            required: true,
            message: '用户名不能为空',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 10,
            message: '用户名必须在3~10个字符之间',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '密码不能为空',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 15,
            message: '密码必须在6~15个字符之间',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    onLogin () {
      // 验证登录表单合法性
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          return
        }
        this.$http.post('/login', this.loginForm).then(res => {
          const {
            data,
            meta
          } = res.data
          if (meta.status !== 200) {
            this.$message.error(meta.msg)
            return
          }
          this.$message.success('登录成功')
          // 登录成功之后，将token放入sessionStorage
          window.sessionStorage.setItem('token', data.token)
          this.$router.push('/home')
        })
      })
    },
    // 重置登录表单
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    }
  }
}
</script>

<!--
使用less定义样式的时候，遇到项目编译报错，提示需要安装less-loader依赖，
通过vue的ui面板先后安装了less-loader和less依赖，启动项目依然报错，
因为less-loader的版本过高导致的，先卸载less-loader然后安装7.x版本即可：
// 卸载
npm uninstall --save less-loader
// 安装
npm install -D less-loader@7.x
-->
<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  //使用flex布局让登录盒子居中
  display: flex;
  justify-content: center;
  align-items: center;
}

//登录盒子
.login_box {
  width: 430px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  // 这4行代码也可以让登录盒子居中，但推荐使用flex布局
  //position: absolute;
  //top: 50%;
  //left: 50%;
  //transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

//登录表单
.login_form {
  height: 100%;
  padding: 0 20px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;

  //登录和重置按钮
  .login_btn {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
